package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.eqp.entity.EqpEquipinfo;
import org.jeecg.eqp.service.IEqpEquipinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 设备主数据
 * @Author: licy
 * @Date: 2022-10-12
 * @Version: V1.0
 */
@Api(tags = "设备主数据")
@RestController
@RequestMapping("/eqp/eqpEquipinfo")
@Slf4j
public class EqpEquipinfoController extends JeecgController<EqpEquipinfo, IEqpEquipinfoService> {
    @Autowired
    private IEqpEquipinfoService eqpEquipinfoService;

    /**
     * 单表分页列表查询
     *
     * @param eqpEquipinfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "设备主数据-分页列表查询")
    @ApiOperation(value = "设备主数据-分页列表查询", notes = "设备主数据-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<EqpEquipinfo>> queryPageList(EqpEquipinfo eqpEquipinfo,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<EqpEquipinfo> queryWrapper = QueryGenerator.initQueryWrapper(eqpEquipinfo, req.getParameterMap());
        Page<EqpEquipinfo> page = new Page<EqpEquipinfo>(pageNo, pageSize);
        IPage<EqpEquipinfo> pageList = eqpEquipinfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param eqpEquipinfo
     * @return
     */
    @AutoLog(value = "设备主数据-添加")
    @ApiOperation(value = "设备主数据-添加", notes = "设备主数据-添加")
    @PostMapping(value = "/add")
    @DS("#header.line")
    public Result<String> add(@RequestBody EqpEquipinfo eqpEquipinfo) {
        eqpEquipinfoService.save(eqpEquipinfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eqpEquipinfo
     * @return
     */
    @AutoLog(value = "设备主数据-编辑")
    @ApiOperation(value = "设备主数据-编辑", notes = "设备主数据-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody EqpEquipinfo eqpEquipinfo) {
        //框架修改
        eqpEquipinfoService.updateById(eqpEquipinfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过map删除
     *
     * @param eqpEquipinfo
     * @return
     */
    @AutoLog(value = "设备主数据-通过id删除")
    @ApiOperation(value = "设备主数据-通过id删除", notes = "设备主数据-通过id删除")
    @RequestMapping(value = "/delete")
    @DS("#header.line")
    public Result<String> delete(@RequestBody EqpEquipinfo eqpEquipinfo) {
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("SERIALNO", map.get("serialno"));
        //自定义查询条件，下面的值全部满足才能删除成功
        eqpEquipinfoService.removeById(eqpEquipinfo);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "设备主数据-批量删除")
    @ApiOperation(value = "设备主数据-批量删除", notes = "设备主数据-批量删除")
    @RequestMapping(value = "/deleteBatch")
    @DS("#header.line")
    public Result<String> deleteBatch(@RequestBody List<EqpEquipinfo> list) {
        list.forEach(item->{
            eqpEquipinfoService.removeById(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "设备主数据-通过id查询")
    @ApiOperation(value = "设备主数据-通过id查询", notes = "设备主数据-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("#header.his")
    public Result<EqpEquipinfo> queryById(@RequestParam(name = "id", required = true) String id) {
        EqpEquipinfo eqpEquipinfo = eqpEquipinfoService.getById(id);
        if (eqpEquipinfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(eqpEquipinfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param eqpEquipinfo
     */
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, EqpEquipinfo eqpEquipinfo) {
        return super.exportXls(request, eqpEquipinfo, EqpEquipinfo.class, "设备主数据");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @DS("#header.line")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EqpEquipinfo.class);
    }

}
